package com.google.gwt.sample.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.google.gwt.sample.client.Language;

import com.google.gwt.sample.client.TextLocalizationService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class TextLocalizationServiceImpl extends RemoteServiceServlet implements TextLocalizationService{
	
	private Map<String,String> dictionary = new HashMap<String, String>();
	
	public TextLocalizationServiceImpl(){}
	
	@Override
	public Map<String, String> getDictionary(Language language) throws IOException {
		Properties prop = new Properties();
		InputStream is = null;
		
		File dir = new File(language.getLanguage()+"_us");
		String[] files = dir.list();
		if(files == null){
			throw new IOException("No files in directory");
		}else{
			for(int i = 0;i < files.length; i++){
		
				try {
					is=new FileInputStream(language.getLanguage()+"_us/"+files[i]);
					prop.load(is);
				} catch(IOException ioe) {
					System.out.println("Error cargando el archivo de propiedades");
					ioe.printStackTrace();	
				}
				Enumeration e = prop.propertyNames();
				while (e.hasMoreElements()) {
					Object obj = e.nextElement();
					dictionary.put(obj.toString(), prop.getProperty(obj.toString()));
				}
		
			}
		}
		return dictionary;
	}
}
